package com.ruoyi.web.controller.weiriya;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.WMember;
import com.ruoyi.weiriya.service.IWMemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户信息Controller
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Api(tags = "用户信息管理")
@RestController
@RequestMapping("/weiriya/member")
public class WMemberController extends BaseController
{
    @Autowired
    private IWMemberService wMemberService;

    /**
     * 查询用户信息列表
     */
    @ApiOperation("获取管理员所辖的用户信息列表")
    @PreAuthorize("@ss.hasPermi('weiriya:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(WMember wMember)
    {
        startPage();
        List<WMember> list = wMemberService.getList(wMember);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @ApiOperation("导出用户信息列表")
    @PreAuthorize("@ss.hasPermi('weiriya:member:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WMember wMember)
    {
        List<WMember> list = wMemberService.getList(wMember);
        ExcelUtil<WMember> util = new ExcelUtil<WMember>(WMember.class);
        util.exportExcel(response, list, "用户信息数据");
    }

    /**
     * 导入用户信息列表
     */
    @ApiOperation("导入用户信息列表")
    @PreAuthorize("@ss.hasPermi('weiriya:member:import')")
    @Log(title = "用户信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<WMember> util = new ExcelUtil<WMember>(WMember.class);
        List<WMember> list = util.importExcel(file.getInputStream());
        return success(wMemberService.saveBatch(list));
    }

    /**
     * 获取用户首页统计信息
     */
    @ApiOperation("获取用户首页统计信息")
    @PreAuthorize("@ss.hasPermi('weiriya:member:query')")
    @GetMapping(value = "/statistics")
    public AjaxResult statistics()
    {
        return success(wMemberService.statistics());
    }

    /**
     * 获取用户信息详细信息
     */
    @ApiOperation("获取用户信息详细")
    @ApiImplicitParam(name = "id", value = "用户信息ID", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('weiriya:member:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(wMemberService.getWMemberById(id));
    }

    /**
     * 新增用户信息
     */
    @ApiOperation("新增用户信息")
    @PreAuthorize("@ss.hasPermi('weiriya:member:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WMember wMember)
    {
        return toAjax(wMemberService.saveWMember(wMember));
    }

    /**
     * 修改用户信息
     */
    @ApiOperation("更新用户信息")
    @PreAuthorize("@ss.hasPermi('weiriya:member:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WMember wMember)
    {
        return toAjax(wMemberService.updateWMemberById(wMember));
    }

    /**
     * vip续费
     */
    @ApiOperation("vip续费")
    @PreAuthorize("@ss.hasPermi('weiriya:member:edit')")
    @Log(title = "vip续费", businessType = BusinessType.UPDATE)
    @PostMapping("/vip")
    public AjaxResult editVip(@RequestBody WMember wMember)
    {
        return toAjax(wMemberService.updateVipById(wMember));
    }

    /**
     * 获取vip续费时间和价格
     */
    @ApiOperation("获取vip续费时间和价格")
    @PreAuthorize("@ss.hasPermi('weiriya:member:edit')")
    @Log(title = "vip续费", businessType = BusinessType.UPDATE)
    @PostMapping("/getVipInfo")
    public AjaxResult getVipInfo(@RequestBody WMember wMember)
    {
        return success(wMemberService.editVip(wMember));
    }



    /**
     * 修改用户头像和昵称
     */
    @ApiOperation("修改用户头像和昵称")
    @PreAuthorize("@ss.hasPermi('weiriya:member:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updateNickNameAndAvatar")
    public AjaxResult updateNickNameAndAvatar(@RequestBody WMember wMember)
    {
        WMember byId = wMemberService.getById(SecurityUtils.getUserId());
        if(StringUtils.isNotBlank(wMember.getNickName())) {
            byId.setNickName(wMember.getNickName());
        }
        if(StringUtils.isNotBlank(wMember.getAvatar())){
            byId.setAvatar(wMember.getAvatar());
        }
        return toAjax(wMemberService.updateById(byId));
    }
    /**
     * 修改用户余额
     */
    @ApiOperation("修改用户余额")
    @PreAuthorize("@ss.hasPermi('weiriya:member:editWallet')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updateMemberWallet")
    public AjaxResult updateMemberWallet(@RequestBody WMember wMember)
    {
        Double refundAmount = wMember.getRefundAmount();
        if(StringUtils.isNull(refundAmount) || refundAmount <= 0){
            throw new ServiceException("请填写合理的退款金额！");
        }
        WMember byId = wMemberService.getById(wMember.getId());
        Double wallet = byId.getWallet();
        if(refundAmount.compareTo(wallet) > 0){
            throw new ServiceException("退款金额不能超过用户余额！");
        }
        byId.setWallet(wallet - refundAmount);
        return toAjax(wMemberService.updateById(byId));
    }

    /**
     * 修改用户状态
     */
    @ApiOperation("修改用户状态")
    @PreAuthorize("@ss.hasPermi('weiriya:member:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping("/state")
    public AjaxResult editState(@RequestBody WMember wMember)
    {
        WMember byId = wMemberService.getById(wMember.getId());
        Integer status = byId.getStatus();
        if(status == 1){
            byId.setStatus(2);
        } else {
            byId.setStatus(1);
        }
        return toAjax(wMemberService.updateById(byId));
    }

    /**
     * 删除用户信息
     */
    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "ids", value = "用户信息ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('weiriya:member:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(wMemberService.removeMemberByIds(ids));
    }
}
